
#include "StdAfx.h"
#include "win32_hwnd.h"
#include <commondef/glb_msg_define.h>
#include <interface/web_service_interface.h>
#include "update_impl.h"
#include "update_msg_def.h"

HWND win32_hwnd::m_hWnd = NULL;
update_impl* win32_hwnd::update_impl_ = NULL;

win32_hwnd::win32_hwnd()
{
	m_hWnd = NULL;
	module_	= NULL;
	update_impl_ = NULL;
}

win32_hwnd::~win32_hwnd()
{
	destory();
}

void 
win32_hwnd::destory()
{
	if( this->m_hWnd != NULL )
	{
		::DestroyWindow( this->m_hWnd );
		this->m_hWnd = NULL;
	}	
	if ( (module_ != NULL) && class_name.size() )
	{
		if( !UnregisterClass(class_name.c_str(),module_) )
		{
			/*MessageBox (NULL, TEXT ("win32_hwnd UnregisterClass  error."), 
				"win32_hwnd", MB_ICONERROR) ;*/
		}
		this->module_ = NULL;
	}

	if (update_impl_ != NULL )
	{
		update_impl_ = NULL;
	}
}

HWND
win32_hwnd::GetSafeHwnd() const
{ 
	return this == NULL ? NULL : m_hWnd; 
}

void 
win32_hwnd::set_update_impl( update_impl * impl_ )
{
	update_impl_ = impl_;
}
int
win32_hwnd::create_hwnd ( HMODULE hModule, LPCTSTR lpszClassName )
{
	if( m_hWnd && ::IsWindow(m_hWnd) )
   	{
   	   return 1;
   	}
	   	
     WNDCLASS     wndclass ;
	 memset(&wndclass,0,sizeof(WNDCLASS));
     wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
     wndclass.lpfnWndProc   = WndProc ;
     wndclass.cbClsExtra    = 0 ;
     wndclass.cbWndExtra    = 0 ;
     wndclass.hInstance     = hModule ;
     wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
     wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
     wndclass.lpszMenuName  = NULL ;
     wndclass.lpszClassName = lpszClassName ;

     if (!RegisterClass (&wndclass))
     {
          //MessageBox (NULL, TEXT ("craete win32_hwnd error."), 
          //            lpszClassName, MB_ICONERROR) ;
          return 0 ;
     }
     
	 module_ = hModule;
	 class_name = lpszClassName;

	 int xscreen = ::GetSystemMetrics(SM_CXSCREEN);
	 int yscreen = ::GetSystemMetrics(SM_CYSCREEN);
	
     m_hWnd = CreateWindow (lpszClassName,                  // window class name
                          NULL, // window caption
                          WS_OVERLAPPEDWINDOW,        // window style 
                          xscreen/2,              // initial x position
                          yscreen/2,              // initial y position
                          0,              // initial x size
                          0,              // initial y size
                          NULL,                       // parent window handle
                          NULL,                       // window menu handle
                          hModule,                  // program instance handle
                          NULL) ;                     // creation parameters
     
	 ::ShowWindow (m_hWnd, SW_HIDE) ; 
     
     return 1;
}


LRESULT 
CALLBACK win32_hwnd::WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
		switch(message)
		{
			case WM_DESTROY:
				{
					PostQuitMessage(0);
					return 0;
				}
				break;
			case WM_UPDATE_ON_LINE_UPDATE_TATA:
				{
					if ( update_impl_ != NULL )
					{
						update_impl_->on_run_trueupdate_client();
					}
					return 1;
				}
				break;
			case WM_GLB_UI_SAVE_SETTING:
				{
					return 1;
				}
				break;
			case WM_TIMER:
				{
				}
				break;
			case WM_GLB_WEB_SVR_TO_UI_FINISH:
				{
					if ( update_impl_ != NULL )
					{
						//update_impl_->on_restor_veb_version((aot::rpc::ipacket*)wParam );
					}
					return 1;
				}
			case WM_UPDATE_LOGGIN_IN_SEARCH_UPDATE_TATA:
				{
					if ( update_impl_ != NULL) 
					{
						//update_impl_->on_login_update_msg(wParam,lParam);
						return 1;
					}
				}
				break;
			case WM_UPDATE_SEARCH_UPDATE_TATA:
				{
					if ( update_impl_ != NULL )
					{
						update_impl_->on_search_update_msg(wParam,lParam);
					}
					return 1;
				}
				break;
			default:
				break;
		}
     return DefWindowProc (hwnd, message, wParam, lParam) ;
}

